#pragma once
#include <cstdio>
#include <cassert>
#include <cstdarg>
#include <ctime>
#include <stdlib.h>
#include <unistd.h>
#define DEBUG 0
#define NOTICE 1
#define WARNINING 2
#define FATAL 3

const char* LEVEL[] = { "DEBUG", "NOTICE", "WARINING", "FATAL" };

void logMessage(int level, const char* format, ...)
{
    assert(level >= DEBUG);
    assert(level <= FATAL);
    char* name = getenv("USER");
    char logInfo[1024] = { 0 };
    va_list ap;
    va_start(ap, format);
    vsnprintf(logInfo, sizeof(logInfo) - 1, format, ap);
    FILE* out = level == FATAL ? stderr : stdout;
    va_end(ap);
    fprintf(out, "%s | %u | %s | %s | %d\n",
        LEVEL[level], 
        (size_t)time(nullptr),
        name == nullptr ? "unknown" : name,
        logInfo,
        getpid());
}